package com.zzx.sentinel.service.impl;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.zzx.sentinel.service.HelloService;
import org.springframework.stereotype.Service;

/**
 * @ClassName: HelloServiceImpl
 * @Description:
 * @Author zhouzhixin
 * @Date 2021/7/20
 * @Version 1.0
 */
@Service
public class HelloServiceImpl implements HelloService {

    @SentinelResource(value = "hello", blockHandler = "blockHandlerException", fallback = "fallbackException")
    @Override
    public String hello(String string) {
        int num = 8 / 0;
        return "hello";
    }

    /**
     * <p>上方@SentinelResource注解指定的限流调用方法</p>
     *
     * @param string
     * @param blockException
     * @return java.lang.String
     * @author zhouzhixin
     * @date 2021/7/20 14:15
     */
    public static String blockHandlerException(String string, BlockException blockException) {
        System.out.println("blockHandlerException: " + blockException);
        return "已被流量控制，请稍后再试";
    }

    /**
     * <p>上方@SentinelResource注解指定的异常熔断调用方法</p>
     *
     * @param
     * @return java.lang.String
     * @author zhouzhixin
     * @date 2021/7/20 16:10
     */
    public static String fallbackException(String string, Throwable throwable) {
        System.out.println("fallbackException: " + throwable);
        return "程序异常，进入备用方法";
    }

}
